package com.hua.yuan.live;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import com.hua.yuan.common.BaseActivity;
import com.hua.yuan.live.repertory.cloud_data.loader.LiveP2pCreateRoomLoader;
import com.hua.yuan.live.repertory.p2p.LiveP2pCreateRoom;
import com.hua.yuan.rtcroom.IRTCRoomListener;
import com.hua.yuan.rtcroom.RTCRoom;
import com.hua.yuan.rtcroom.sdk.PusherInfo;
import com.tencent.rtmp.TXLiveConstants;
import com.tencent.rtmp.TXLivePlayConfig;
import com.tencent.rtmp.TXLivePlayer;
import com.tencent.rtmp.ui.TXCloudVideoView;

import java.util.List;

/**
 * 视频通话
 */
public class LiveVideoPhoneActivity extends BaseActivity
        implements IRTCRoomListener,
        View.OnClickListener,
        LiveP2pCreateRoom.IMessageCallBack {


    private TXCloudVideoView mPlayVideoView;

    private RTCRoom mRtcRoom;

    private TXCloudVideoView mRemoteVideoView;

    private TXLivePlayer mLivePlayer;


    private ImageView ivExitPhone;

    private Handler mHandler = new Handler();

    private final static String TAG = LiveVideoPhoneActivity.class.getName();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_room_video);
        mPlayVideoView = findViewById(R.id.video_local_room_live);
        mRemoteVideoView = findViewById(R.id.video_remote_room_live);
        mRemoteVideoView.setVisibility(View.GONE);
        ivExitPhone = findViewById(R.id.iv_exit_room_live);
        mRtcRoom = LiveModule.getRtcRoom(getApplicationContext());
        ivExitPhone.setOnClickListener(this);
        mRtcRoom.setRTCRoomListener(this);
        //视频布局
        mLivePlayer = new TXLivePlayer(this);
        mLivePlayer.setPlayerView(mRemoteVideoView);
        mLivePlayer.setConfig(new TXLivePlayConfig());
        mLivePlayer.enableHardwareDecode(true);
        mLivePlayer.setRenderMode(TXLiveConstants.RENDER_MODE_FULL_FILL_SCREEN);
        final String anchorRtcUserI = getIntent().getStringExtra(LiveModule.INTENT_ANCHOR_RTC_USER_ID);
        if (!TextUtils.isEmpty(anchorRtcUserI)) {
            LiveP2pCreateRoomLoader.createRoom(anchorRtcUserI, mRtcRoom, this);
        }

    }

    @Override
    public void onBackPressed() {
        mRtcRoom.stopLocalPreview();
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mRtcRoom.switchToForeground();
    }

    @Override
    public void onGetPusherList(List<PusherInfo> pusherList) {
        Log.i(TAG, "pusherList");
    }

    @Override
    public void onPusherJoin(final PusherInfo pusherInfo) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), "开始推流，远程好友", Toast.LENGTH_SHORT).show();
                Log.i(TAG, "onPusherJoin");
                if (!mLivePlayer.isPlaying()) {
                    mRemoteVideoView.setVisibility(View.VISIBLE);
                    int result=mLivePlayer.startPlay(pusherInfo.accelerateURL,TXLivePlayer.PLAY_TYPE_LIVE_RTMP_ACC);
                    if (result != 0){
                        Log.i(TAG,String.format("[BaseRoom] 播放成员 {%s} 地址 {%s} 失败", pusherInfo.userID, pusherInfo.accelerateURL));
                    }
                }
            }
        });

//        mRtcRoom.addRemoteView(mRemoteVideoView, pusherInfo, new RTCRoom.RemoteViewPlayCallback() {
//            @Override
//            public void onPlayBegin() {
//                runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        Toast.makeText(getApplicationContext(), "开始推流，远程好友", Toast.LENGTH_SHORT).show();
//                    }
//                });
//            }
//
//            @Override
//            public void onPlayError() {
//
//            }
//        });

    }

    @Override
    public void onPusherQuit(PusherInfo pusherInfo) {
        mRtcRoom.deleteRemoteView(pusherInfo);
        Log.i(TAG, "onPusherQuit");
    }

    @Override
    public void onRecvRoomTextMsg(String roomID, String userID, String userName, String userAvatar, String message) {
        Log.i(TAG, "onRecvRoomTextMsg");
    }

    @Override
    public void onRecvRoomCustomMsg(String roomID, String userID, String userName, String userAvatar, String cmd, String message) {
        Log.i(TAG, "onRecvRoomCustomMsg");
    }

    @Override
    public void onRoomClosed(String roomID) {

    }

    @Override
    public void onDebugLog(String log) {

    }

    @Override
    public void startPush() {
        mRtcRoom.startLocalPreview(mPlayVideoView);
    }

    @Override
    public void onError(int errorCode, String errorMessage) {
        Toast.makeText(getApplicationContext(), "当前主播不存在，无法进入视频聊天", Toast.LENGTH_SHORT).show();
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        }, 20);
    }

    @Override
    public void onSuccess() {
        Toast.makeText(getApplicationContext(), "等待主播进入", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onClick(View v) {

        if (R.id.iv_exit_room_live == v.getId()) {
            onBackPressed();
        }
    }
}
